package com.suneast.suneaststore.modules.sort.adapter;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.sort.ItemBean;
import com.suneast.suneaststore.bean.sort.SubCate;
import com.suneast.suneaststore.util.log.LatteLogger;

import java.util.ArrayList;

/**
 * Created by zestz on 2018/3/7.
 */

public class SortAdapter extends BaseQuickAdapter<ItemBean, SortAdapter.ViewHolder> {

    public SortAdapter(int layoutId) {
        super(layoutId);
    }

    public SortAdapter(int layoutId, ArrayList<ItemBean> list) {
        super(layoutId, list);
    }

    @Override
    protected void convert(ViewHolder helper, ItemBean item) {

        helper.setText(R.id.category_name_tv, item.getCurrent().getName());
        TextView tv = helper.getView(R.id.tv_show_all_category);
        LatteLogger.d("test", "isShowAll = " + item.getCurrent().isShowAll());

        tv.setVisibility(
                item.getCurrent().isShowAll() ? View.VISIBLE : View.INVISIBLE);
        RecyclerView rv = helper.getView(R.id.category_item_recycler_view);
        rv.setLayoutManager(
                new GridLayoutManager(
                        helper.itemView.getContext(),
                        3, GridLayoutManager.VERTICAL, false));
        SortItemAdapter adapter =
                new SortItemAdapter(R.layout.item_category_item, item.getSubCateList());
        rv.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mItemClickListener != null) {
                    SubCate bean = (SubCate) adapter.getData().get(position);
                    mItemClickListener.onItemClick(bean);
                }
            }
        });
        helper.addOnClickListener(R.id.tv_show_all_category);
    }

    public static class ViewHolder extends BaseViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    private ItemClickListener mItemClickListener;

    public void setItemClickListener(SortAdapter.ItemClickListener listener) {
        mItemClickListener = listener;
    }

    public interface ItemClickListener {
        void onItemClick(SubCate cate);
    }

}
